import { ethers } from 'ethers';

// BSC LaunchPad 合约配置
export const BSC_CONTRACT_CONFIG = {
  address: '0x031b8B821B0F1A87D1a1451479d29660Bc68d2C8', // 修复卖出逻辑的合约地址
  chainId: 97, // BSC Testnet
  rpcUrl: 'https://data-seed-prebsc-1-s1.binance.org:8545/',
  
  // PancakeSwap Router (测试网)
  PANCAKE_ROUTER: '0xD99D1c33F9fC3444f8101754aBC46c52416550D1',
  
  // 合约常量
  CONSTANTS: {
    MIN_TARGET: ethers.parseEther('1'),      // 1 BNB
    MAX_TARGET: ethers.parseEther('100'),    // 100 BNB
    LAUNCH_DURATION: 7 * 24 * 60 * 60,      // 7 days
    GRADUATION_THRESHOLD: 80,                // 80%
    PLATFORM_FEE: 200,                      // 2%
    CURVE_BASE_PRICE: ethers.parseEther('0.0001'),     // 0.0001 BNB
    CURVE_PRICE_INCREMENT: ethers.parseEther('0.00001'), // 0.00001 BNB
    TOKENS_FOR_CURVE: ethers.parseEther('800000000'),   // 800M tokens
    TOKENS_FOR_LIQUIDITY: ethers.parseEther('200000000'), // 200M tokens
    CREATION_FEE: ethers.parseEther('0.01')  // 0.01 BNB
  }
};

// BSC LaunchPad 合约 ABI
export const BSC_LAUNCHPAD_ABI = [
  {
    "inputs": [
      {"internalType": "address", "name": "_pancakeRouter", "type": "address"}
    ],
    "stateMutability": "nonpayable",
    "type": "constructor"
  },
  {
    "anonymous": false,
    "inputs": [
      {"indexed": true, "internalType": "uint256", "name": "launchId", "type": "uint256"},
      {"indexed": true, "internalType": "address", "name": "creator", "type": "address"}
    ],
    "name": "LaunchCancelled",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {"indexed": true, "internalType": "uint256", "name": "launchId", "type": "uint256"},
      {"indexed": true, "internalType": "address", "name": "contributor", "type": "address"},
      {"indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256"}
    ],
    "name": "RefundClaimed",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {"indexed": true, "internalType": "uint256", "name": "launchId", "type": "uint256"},
      {"indexed": true, "internalType": "address", "name": "tokenAddress", "type": "address"},
      {"indexed": false, "internalType": "address", "name": "pairAddress", "type": "address"},
      {"indexed": false, "internalType": "uint256", "name": "liquidityBNB", "type": "uint256"},
      {"indexed": false, "internalType": "uint256", "name": "liquidityTokens", "type": "uint256"}
    ],
    "name": "TokenGraduated",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {"indexed": true, "internalType": "uint256", "name": "launchId", "type": "uint256"},
      {"indexed": true, "internalType": "address", "name": "creator", "type": "address"},
      {"indexed": false, "internalType": "address", "name": "tokenAddress", "type": "address"},
      {"indexed": false, "internalType": "string", "name": "name", "type": "string"},
      {"indexed": false, "internalType": "string", "name": "symbol", "type": "string"},
      {"indexed": false, "internalType": "uint256", "name": "targetAmount", "type": "uint256"}
    ],
    "name": "TokenLaunchCreated",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {"indexed": true, "internalType": "uint256", "name": "launchId", "type": "uint256"},
      {"indexed": true, "internalType": "address", "name": "buyer", "type": "address"},
      {"indexed": false, "internalType": "uint256", "name": "bnbAmount", "type": "uint256"},
      {"indexed": false, "internalType": "uint256", "name": "tokensReceived", "type": "uint256"},
      {"indexed": false, "internalType": "uint256", "name": "newPrice", "type": "uint256"}
    ],
    "name": "TokenPurchase",
    "type": "event"
  },
  {
    "anonymous": false,
    "inputs": [
      {"indexed": true, "internalType": "uint256", "name": "launchId", "type": "uint256"},
      {"indexed": true, "internalType": "address", "name": "seller", "type": "address"},
      {"indexed": false, "internalType": "uint256", "name": "tokensAmount", "type": "uint256"},
      {"indexed": false, "internalType": "uint256", "name": "bnbReceived", "type": "uint256"},
      {"indexed": false, "internalType": "uint256", "name": "newPrice", "type": "uint256"}
    ],
    "name": "TokenSale",
    "type": "event"
  },
  {
    "inputs": [{"internalType": "uint256", "name": "_launchId", "type": "uint256"}],
    "name": "buyTokens",
    "outputs": [],
    "stateMutability": "payable",
    "type": "function"
  },
  {
    "inputs": [{"internalType": "uint256", "name": "_launchId", "type": "uint256"}],
    "name": "cancelLaunch",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [{"internalType": "uint256", "name": "_launchId", "type": "uint256"}],
    "name": "claimRefund",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {"internalType": "string", "name": "_name", "type": "string"},
      {"internalType": "string", "name": "_symbol", "type": "string"},
      {"internalType": "string", "name": "_description", "type": "string"},
      {"internalType": "string", "name": "_website", "type": "string"},
      {"internalType": "string", "name": "_telegram", "type": "string"},
      {"internalType": "string", "name": "_twitter", "type": "string"},
      {"internalType": "uint256", "name": "_targetAmount", "type": "uint256"}
    ],
    "name": "createTokenLaunch",
    "outputs": [],
    "stateMutability": "payable",
    "type": "function"
  },
  {
    "inputs": [
      {"internalType": "uint256", "name": "_launchId", "type": "uint256"},
      {"internalType": "uint256", "name": "_bnbAmount", "type": "uint256"}
    ],
    "name": "getBuyPreview",
    "outputs": [
      {"internalType": "uint256", "name": "tokensReceived", "type": "uint256"},
      {"internalType": "uint256", "name": "newPrice", "type": "uint256"}
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [{"internalType": "uint256", "name": "_launchId", "type": "uint256"}],
    "name": "getContributors",
    "outputs": [{"internalType": "address[]", "name": "", "type": "address[]"}],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [{"internalType": "uint256", "name": "_launchId", "type": "uint256"}],
    "name": "getCurrentTokenPrice",
    "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [{"internalType": "uint256", "name": "_launchId", "type": "uint256"}],
    "name": "getLaunchInfo",
    "outputs": [
      {"internalType": "address", "name": "tokenAddress", "type": "address"},
      {"internalType": "address", "name": "creator", "type": "address"},
      {"internalType": "string", "name": "name", "type": "string"},
      {"internalType": "string", "name": "symbol", "type": "string"},
      {"internalType": "string", "name": "description", "type": "string"},
      {"internalType": "uint256", "name": "targetAmount", "type": "uint256"},
      {"internalType": "uint256", "name": "raisedAmount", "type": "uint256"},
      {"internalType": "uint256", "name": "deadline", "type": "uint256"},
      {"internalType": "bool", "name": "launched", "type": "bool"},
      {"internalType": "bool", "name": "cancelled", "type": "bool"},
      {"internalType": "uint256", "name": "currentPrice", "type": "uint256"},
      {"internalType": "uint256", "name": "tokensSold", "type": "uint256"},
      {"internalType": "uint256", "name": "totalSupplyForSale", "type": "uint256"}
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [{"internalType": "uint256", "name": "_launchId", "type": "uint256"}],
    "name": "getLaunchSocialInfo",
    "outputs": [
      {"internalType": "string", "name": "website", "type": "string"},
      {"internalType": "string", "name": "telegram", "type": "string"},
      {"internalType": "string", "name": "twitter", "type": "string"}
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {"internalType": "uint256", "name": "_launchId", "type": "uint256"},
      {"internalType": "uint256", "name": "_tokenAmount", "type": "uint256"}
    ],
    "name": "getSellPreview",
    "outputs": [
      {"internalType": "uint256", "name": "bnbReceived", "type": "uint256"},
      {"internalType": "uint256", "name": "newPrice", "type": "uint256"}
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [
      {"internalType": "uint256", "name": "_launchId", "type": "uint256"},
      {"internalType": "address", "name": "_user", "type": "address"}
    ],
    "name": "getUserContribution",
    "outputs": [
      {"internalType": "uint256", "name": "", "type": "uint256"},
      {"internalType": "uint256", "name": "", "type": "uint256"}
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [{"internalType": "address", "name": "_user", "type": "address"}],
    "name": "getUserLaunches",
    "outputs": [{"internalType": "uint256[]", "name": "", "type": "uint256[]"}],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [{"internalType": "uint256", "name": "_launchId", "type": "uint256"}],
    "name": "manualGraduateToken",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [
      {"internalType": "uint256", "name": "_launchId", "type": "uint256"},
      {"internalType": "uint256", "name": "_tokenAmount", "type": "uint256"}
    ],
    "name": "sellTokens",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  }
];

// 创建合约实例的工具函数
export class BSCContractHelper {
  private provider: ethers.JsonRpcProvider;
  private contract: ethers.Contract;

  constructor() {
    this.provider = new ethers.JsonRpcProvider(BSC_CONTRACT_CONFIG.rpcUrl);
    this.contract = new ethers.Contract(
      BSC_CONTRACT_CONFIG.address,
      BSC_LAUNCHPAD_ABI,
      this.provider
    );
  }

  // 获取只读合约实例
  getReadOnlyContract() {
    return this.contract;
  }

  // 获取连接钱包的合约实例
  getContractWithSigner(privateKey: string) {
    const wallet = new ethers.Wallet(privateKey, this.provider);
    return this.contract.connect(wallet);
  }

  // 获取钱包实例
  getWallet(privateKey: string) {
    return new ethers.Wallet(privateKey, this.provider);
  }

  // 获取provider
  getProvider() {
    return this.provider;
  }

  // 格式化地址
  static formatAddress(address: string): string {
    return ethers.getAddress(address);
  }

  // 格式化BNB数量
  static formatBNB(amount: string): string {
    return ethers.formatEther(amount);
  }

  // 解析BNB数量
  static parseBNB(amount: string): bigint {
    return ethers.parseEther(amount);
  }

  // 格式化代币数量
  static formatTokens(amount: string): string {
    return ethers.formatEther(amount);
  }

  // 解析代币数量
  static parseTokens(amount: string): bigint {
    return ethers.parseEther(amount);
  }
}

export default BSCContractHelper;
